{% extends 'mobile_app/base.html' %}
{% load static %}

{% block title %}切换号码 - 郑州移不动{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">
                <i class="fas fa-exchange-alt text-primary me-2"></i>切换查询号码
            </h2>
        </div>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-md-8 col-lg-6">
        <!-- 当前主号码 -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">
                    <i class="fas fa-star me-2"></i>当前主号码
                </h5>
            </div>
            <div class="card-body">
                {% for phone in phone_numbers %}
                    {% if phone.is_primary %}
                    <div class="d-flex align-items-center">
                        <div class="me-3">
                            <i class="fas fa-phone text-primary" style="font-size: 2rem;"></i>
                        </div>
                        <div class="flex-grow-1">
                            <h4 class="mb-1">{{ phone.number }}</h4>
                            <p class="text-muted mb-0">
                                余额：¥{{ phone.balance.amount|floatformat:2 }} | 
                                流量：{{ phone.balance.data_remaining }}MB | 
                                语音：{{ phone.balance.voice_remaining }}分钟
                            </p>
                        </div>
                        <div>
                            <span class="badge bg-primary fs-6">主号码</span>
                        </div>
                    </div>
                    {% endif %}
                {% endfor %}
            </div>
        </div>

        <!-- 号码列表 -->
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-light">
                <h5 class="mb-0">
                    <i class="fas fa-list me-2"></i>选择要查询的号码
                </h5>
            </div>
            <div class="card-body">
                {% if phone_numbers %}
                    <form method="post">
                        {% csrf_token %}
                        <div class="row g-3">
                            {% for phone in phone_numbers %}
                            <div class="col-12">
                                <div class="card h-100 phone-option {% if phone.is_primary %}border-primary bg-light{% endif %}" 
                                     style="cursor: pointer; transition: all 0.3s ease;">
                                    <div class="card-body">
                                        <div class="form-check">
                                            <input class="form-check-input" type="radio" name="phone_id" 
                                                   value="{{ phone.id }}" id="phone{{ phone.id }}"
                                                   {% if phone.is_primary %}checked{% endif %}>
                                            <label class="form-check-label w-100" for="phone{{ phone.id }}">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <div>
                                                        <h6 class="mb-1">
                                                            <i class="fas fa-phone me-2"></i>{{ phone.number }}
                                                            {% if phone.is_primary %}
                                                                <span class="badge bg-primary ms-2">主号码</span>
                                                            {% endif %}
                                                        </h6>
                                                        <div class="row text-muted small">
                                                            <div class="col-4">
                                                                <i class="fas fa-wallet me-1"></i>¥{{ phone.balance.amount|floatformat:2 }}
                                                            </div>
                                                            <div class="col-4">
                                                                <i class="fas fa-wifi me-1"></i>{{ phone.balance.data_remaining }}MB
                                                            </div>
                                                            <div class="col-4">
                                                                <i class="fas fa-phone me-1"></i>{{ phone.balance.voice_remaining }}分钟
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="text-end">
                                                        {% if phone.phone_plan %}
                                                            <small class="text-success">{{ phone.phone_plan.plan.name }}</small><br>
                                                            <small class="text-muted">¥{{ phone.phone_plan.plan.monthly_fee }}/月</small>
                                                        {% else %}
                                                            <small class="text-warning">未选择套餐</small>
                                                        {% endif %}
                                                    </div>
                                                </div>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                        
                        <div class="text-center mt-4">
                            <button type="submit" class="btn btn-primary btn-lg">
                                <i class="fas fa-exchange-alt me-2"></i>切换到此号码
                            </button>
                        </div>
                    </form>
                {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-phone-slash text-muted mb-4" style="font-size: 4rem;"></i>
                        <h5 class="text-muted mb-3">还没有添加手机号码</h5>
                        <p class="text-muted mb-4">请先添加您的手机号码，然后就可以切换查询了</p>
                        <a href="{% url 'mobile_app:phone_management' %}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>添加号码
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- 快速操作 -->
        {% if phone_numbers %}
        <div class="card border-0 shadow-sm mt-4">
            <div class="card-header bg-success text-white">
                <h6 class="mb-0">
                    <i class="fas fa-bolt me-2"></i>快速操作
                </h6>
            </div>
            <div class="card-body">
                <div class="row g-2">
                    <div class="col-6">
                        <a href="{% url 'mobile_app:phone_management' %}" class="btn btn-outline-primary w-100">
                            <i class="fas fa-cog me-1"></i>号码管理
                        </a>
                    </div>
                    <div class="col-6">
                        <a href="{% url 'mobile_app:dashboard' %}" class="btn btn-outline-success w-100">
                            <i class="fas fa-tachometer-alt me-1"></i>返回仪表板
                        </a>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 使用说明 -->
        <div class="card border-0 bg-info text-white mt-4">
            <div class="card-body">
                <h6 class="mb-3">
                    <i class="fas fa-info-circle me-2"></i>使用说明
                </h6>
                <ul class="mb-0 small">
                    <li>选择要查询的号码后，该号码将成为您的主号码</li>
                    <li>主号码将在仪表板中显示，方便快速查看余额和套餐信息</li>
                    <li>您可以随时切换主号码，不影响其他号码的使用</li>
                    <li>每个号码的余额、套餐等信息都是独立管理的</li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 号码选择效果
    $('.phone-option').on('click', function() {
        var radio = $(this).find('input[type="radio"]');
        
        // 取消其他选择
        $('.phone-option').removeClass('border-primary bg-light');
        $('input[type="radio"]').prop('checked', false);
        
        // 选择当前号码
        $(this).addClass('border-primary bg-light');
        radio.prop('checked', true);
    });
    
    // 初始化已选择的号码
    $('input[type="radio"]:checked').closest('.phone-option').addClass('border-primary bg-light');
    
    // 表单提交确认
    $('form').on('submit', function(e) {
        var selectedPhone = $('input[name="phone_id"]:checked');
        if (selectedPhone.length === 0) {
            e.preventDefault();
            alert('请选择一个号码');
            return false;
        }
        
        var phoneNumber = selectedPhone.closest('.phone-option').find('h6').text().trim();
        if (!confirm('确定要切换到 "' + phoneNumber + '" 吗？')) {
            e.preventDefault();
            return false;
        }
    });
    
    // 号码卡片悬停效果
    $('.phone-option').on('mouseenter', function() {
        if (!$(this).hasClass('border-primary')) {
            $(this).addClass('border-secondary');
        }
    }).on('mouseleave', function() {
        $(this).removeClass('border-secondary');
    });
});
</script>

<style>
.phone-option {
    border: 2px solid transparent;
    transition: all 0.3s ease;
}

.phone-option:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1) !important;
}

.phone-option.border-primary {
    border-color: var(--primary-color) !important;
    background-color: rgba(0, 123, 255, 0.05) !important;
}

.form-check-input:checked {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}
</style>
{% endblock %}
